<?php
class Article_Type_Action extends BaseAction
{
    public function search()
    {
        $this->getController()->load->library('pagination');
        $data = array("articleTypes" => $this->getModel()->search(), "menu" => "articleType", "pagination" => $this->getController()->pagination, "template" => "articleType_search.php");
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }

    public function delete()
    {
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $this->getController()->load->model("Article_Model");
            if ($this->getController()->Article_Model->paginationCount($id) > 0) {
                die("existArticle");
            }
            else {
                $this->getModel()->id = $id;
                $this->getModel()->delete();
                die("success");
            }
        }
    }

    /**
     *添加文章
     */
    public function add()
    {
        $data = array("template" => "articleType_add.php", "menu" => "articleType", "parentType" => $this->getModel()->listParents());
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
        /*  $array = array("name" => "测试5", "id" => 5);
        $this->getModel()->update($array);*/
    }

    public function edit()
    {
        $this->getModel()->setId($_GET['id']);
        $data = array("template" => "articleType_add.php", "menu" => "articleType", "parentType" => $this->getModel()->listParents(), "domain" => $this->getModel()->load());
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }

    public function saveAdd()
    {
        $message = "";
        if (isset($_POST['id'])) {
            $message = "更新成功";
            $this->getModel()->update($_POST);
        } else {
            $message = "添加成功";
            $this->getModel()->insert($_POST);
        }

        $data = array("template" => "articleType_add.php", "menu" => "articleType", "message" => $message, "parentType" => $this->getModel()->listParents());
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }


}